//
//  JCStatusToolBar.swift
//  SinaWeibo_18
//
//  Created by mac on 15/10/17.
//  Copyright © 2015年 cz. All rights reserved.
//

import UIKit

class JCStatusToolBar: UIView {
    
    var status: JCStatus? {
        didSet{
           setStatus()
        }
    }

    //按钮数组
    private lazy var buttons: [UIButton] = [UIButton]()
    //分割线数组
    private lazy var splites: [UIImageView] = [UIImageView]()
    
    //转发按钮
    private var repostsButton: UIButton?
    //评论
    private var commentsButton: UIButton?
    //赞
    private var attitudesButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加button
       repostsButton = addButton(name: "timeline_icon_retweet", title: "转发")
       commentsButton = addButton(name: "timeline_icon_comment", title: "评论")
       attitudesButton = addButton(name: "timeline_icon_unlike", title: "赞")
        
        //添加分割线
        addSpliteView()
        addSpliteView()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 添加button
    private func addButton(name imageName: String,title: String) -> UIButton{
        let button = UIButton()
        //图片
        button.setImage(UIImage(named: imageName), forState: .Normal)
        //背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        //文字
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(RGB(r: 80, g: 80, b: 80), forState: UIControlState.Normal)
        //字体
        button.titleLabel?.font = UIFont.systemFontOfSize(CELL_STATUS_TEXT_FONT)
        //偏移
        button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5)
        
        addSubview(button)
        //添加到数组
        buttons.append(button)
        
        return button
    }
    // MARK: - 添加分割线
    private func addSpliteView(){
        let spliteImageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(spliteImageView)
        
        //添加到数组
        splites.append(spliteImageView)
    }
    // MARK: - 设置按钮数据
    private func setStatus(){
        
        if let st = status {
//            st.reposts_count = 10220
            setBtnCount(repostsButton!, count: st.reposts_count, defaultTitle: "转发")
            setBtnCount(commentsButton!, count: st.comments_count, defaultTitle: "评论")
            setBtnCount(attitudesButton!, count: st.attitudes_count, defaultTitle: "赞")
        }
    }
    //设置数字
    private func setBtnCount(button: UIButton,count: Int,defaultTitle: String){
        
        if count != 0{
            if count < 10000{
                button.setTitle("\(count)", forState: UIControlState.Normal)
            }else{
                //10110 --> 1万
                //11240 --> 1.1万
                let result = count/1000
                var resultStr = "\(CGFloat(result) / CGFloat(10))万"
                //判断替换 字符串
                resultStr = resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                button.setTitle(resultStr, forState: UIControlState.Normal)
            }
        }else{
            button.setTitle(defaultTitle, forState: UIControlState.Normal)
        }
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let buttonWidth = ScreenWidth/CGFloat(buttons.count)
        //按钮
        for i in 0..<buttons.count{
            let button = buttons[i]
            button.size = CGSizeMake(buttonWidth, height)
            button.x = CGFloat(i) * buttonWidth
        }
        //分割线
        for i in 0..<splites.count{
            let splite = splites[i]
            splite.centerX = CGFloat(i + 1) * buttonWidth
        }
    }
}
